package com.geekaca.db;

import com.geekaca.db.mapper.GoodMapper;
import com.geekaca.db.pojo.Good;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Scanner;

public class TestGoods {

    private static Scanner scanner;

    public static void main(String[] args) throws InterruptedException {
        scanner = new Scanner(System.in);

//        insertGood();
//        nameBlurSearch();
        multiBlurQuery();

    }

    private static void multiBlurQuery() throws InterruptedException {
        SqlSession sqlSession = openSession();
        GoodMapper goodMapper = sqlSession.getMapper(GoodMapper.class);
        Good good = new Good();

        System.out.println("请输入商品ID: ");
        String gid = scanner.nextLine();

        Integer goodId = null;
        if (gid != "") {
            goodId = Integer.parseInt(gid);
        }

        Scanner sc2 = new Scanner(System.in);
        System.out.println("输入商品名称:");


        String title = sc2.nextLine();

        Scanner sc3 = new Scanner(System.in);
        System.out.println("输入商品价格: ");
        String gPrice = sc3.nextLine();


        Double price = null;
        if (gPrice != "") {
//            price = Double.parseDouble(gPrice);
            try {
                // 步骤3：尝试转换为整数
                int integerValue = Integer.parseInt(gPrice);
                System.out.println("是整数字面量，值为：" + integerValue);
            } catch (NumberFormatException e) {
                try {
                    // 步骤4：尝试转换为浮点数
                    double doubleValue = Double.parseDouble(gPrice);
                    System.out.println("是浮点数字面量，值为：" + doubleValue);
                } catch (NumberFormatException ex) {
                    // 步骤5：不是字面量
                    System.out.println("不是字面量。");
                }
            }


        }


        good.setId(goodId);
        good.setTitle(title);
        good.setPrice(price);
        List<Good> goods = goodMapper.goodMultiBlurQuery(good);
        if (goods != null) {
            for (int i = 0; i < goods.size(); i++) {
                System.out.println(goods.get(i));
            }
        }


    }

    private static void insertGood() {
        SqlSession sqlSession = openSession();
        GoodMapper goodMapper = sqlSession.getMapper(GoodMapper.class);

//        Good good = new Good(null,"小米2S", 1999.0);
        System.out.println("请输入商品名称: ");
        String title = scanner.next();
        System.out.println("请输入商品价格: ");
        double price = scanner.nextDouble();

        Good good = new Good(null, title, price);

        int insetredNum = goodMapper.insertGood(good);
        if (insetredNum > 0) {
            System.out.println("执行insert, 受影响的记录数: " + insetredNum);
        }

        sqlSession.close();

    }

    private static SqlSession openSession() {
        //1. 加载mybatis的核心配置文件,获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream ips = null;
        try {
            ips = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(ips);
        //2. 获取SqlSession 对象, 用农来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        return sqlSession;
    }

    public static List<Good> nameBlurSearch() {
        SqlSession sqlSession = openSession();
        GoodMapper goodMapper = sqlSession.getMapper(GoodMapper.class);
        Good good = new Good();
        System.out.println("输入要查询的商品名称: ");
        String title = scanner.next();
        good.setTitle(title);

        List<Good> goods = goodMapper.nameBlurQuery(good);
        if (goods != null) {
            goods.forEach(gd -> System.out.println(gd));
        }

        return goods;

    }
}
